
function TableController(ControlName)
{
//R/W属性，且应该在对象初始化时赋值
	this.objTable=null;
	this.objCheckboxSet=null;						//可选，有CheckBoxs时使用
	
//method
	this.SelectARow=TableController_SelectARow;		//选中指定行,语法：obj.SelectARow(RowIndex);
	this.CheckAll=TableController_CheckAll;			//全选,语法：obj.CheckAll(bFlag);
	this.getItem=TableController_getItem;		    //取得指定列,当前行(指定行)的值,语法：obj.getItem(iField,[RowIndex]);
	this.setItem=TableController_setItem;		    //修改指定列,当前行(指定行)的值,语法：obj.setItem(iField,Value,[RowIndex]);
	this.getItemCSV=TableController_getItemCSV;		//取得指定列,选中行的值,语法：obj.getItemCSV(iField);
	this.getObject=TableController_getObject;		//取得指定列,当前行(指定行)对象,语法：obj.getObject(iField,[RowIndex]);
	this.findObject=TableController_findObject;		//查找指定列,返回符合条件的行号,语法：obj.findField(iField,Value);
	this.Count=TableController_getCount;			//行数


//Read Only
	this.getSelectedCount=TableController_getSelectedCount	//取得选中的行数
	this.CurrentRowIndex=0;
	this.OnAfterRowSelected=null;
}
function TableController_getCount()
{
	return this.objTable.rows.length;
}
function TableController_SelectARow(RowIndex)
{
	for(var i=0;i<this.Count();i++)
	{
		this.objTable.rows[i].bgColor='#ffffff';
	}
	this.objTable.rows.item(RowIndex).bgColor='#DAE1F3';
	this.CurrentRowIndex=RowIndex;
	if (this.OnAfterRowSelected)
	{
		this.OnAfterRowSelected(this.CurrentRowIndex);
	}
	return RowIndex;
}
function TableController_CheckAll(bFlag)
{
	for (i=0;i<this.Count();i++)
	{
		this.objCheckboxSet[i].checked=bFlag;
	}
	return bFlag;
}
function TableController_getItem(iField,RowIndex)
{
	var undefined;
	RowIndex=(RowIndex>=0)?RowIndex:this.CurrentRowIndex;
	if(this.objTable.rows.item(RowIndex).cells[iField].firstChild)
	{
		if("value" in this.objTable.rows.item(RowIndex).cells[iField].firstChild){
			return this.objTable.rows.item(RowIndex).cells[iField].firstChild.value;
		}
		switch(this.objTable.rows.item(RowIndex).cells[iField].firstChild.tagName)
		{
			case "INPUT":
				return this.objTable.rows.item(RowIndex).cells[iField].firstChild.value;
			case "IMG":
				return this.objTable.rows.item(RowIndex).cells[iField].firstChild.src; 
			case "A":
				return this.objTable.rows.item(RowIndex).cells[iField].firstChild.innerText; 
			case "SPAN":
				return this.objTable.rows.item(RowIndex).cells[iField].firstChild.innerText; 
			default : 
				return this.objTable.rows.item(RowIndex).cells[iField].innerText; 
		}
	}
	else
	{
		return "";
	}
}
function TableController_setItem(iField,Value,RowIndex)
{
	RowIndex=(RowIndex>=0)?RowIndex:this.CurrentRowIndex;
	if(this.objTable.rows.item(RowIndex).cells[iField].firstChild)
	{
		switch(this.objTable.rows.item(RowIndex).cells[iField].firstChild.tagName)
		{
			case "INPUT":
				this.objTable.rows.item(RowIndex).cells[iField].firstChild.value=Value;
				break;
			case "IMG":
				this.objTable.rows.item(RowIndex).cells[iField].firstChild.src=Value; 
				break;
			case "A":
				this.objTable.rows.item(RowIndex).cells[iField].firstChild.innerText=Value; 
				break;
			case "SPAN":
				this.objTable.rows.item(RowIndex).cells[iField].firstChild.innerText=Value; 
				break;
			default :
				this.objTable.rows.item(RowIndex).cells[iField].innerText=Value; 
		}
	}
}

function TableController_getItemCSV(iField)
{
	var csv="";
	iField=(iField>=0)?iField:0;
	for (i=0;i<this.Count();i++)
	{
		if(this.objCheckboxSet[i].checked)
		{
			value=this.getItem(iField,i);
			csv=(csv)?(csv+","+value):value;
		}
	}
	return csv;
}
function TableController_getSelectedCount()
{
	var iCount=0;
	for (i=0;i<this.Count();i++)
	{
		if(this.objCheckboxSet[i].checked)
		{
			iCount++;
		}
	}
	return iCount;
}
function TableController_getObject(iField,RowIndex)
{
	RowIndex=(RowIndex>=0)?RowIndex:this.CurrentRowIndex;
	if(this.objTable.rows.item(RowIndex).cells[iField].firstChild)
	{
		return this.objTable.rows.item(RowIndex).cells[iField].firstChild; 
	}
	else
	{
		return null;
	}
}

function TableController_findObject(iField,Value){
	for (i=0;i<this.Count();i++)
	{
		if(this.getItem(iField,i)==Value)
		{
			return i
		}
	}
	return -1;
}
